package patrol.mobile.com.mobilepatrol.activity.office;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Toast;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListWithSearchActivity;
import patrol.mobile.com.mobilepatrol.adapter.NewsListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.NewsListBean;
import patrol.mobile.com.mobilepatrol.bean.OtherBusinessBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

import static patrol.mobile.com.mobilepatrol.activity.CommonListActivity.ROWS;

public class NewsListActivity extends CommonListWithSearchActivity {

    public static final String TITLE = "TITLE";
    public static final String TITLE_STRING = "TITLE_STRING";
    private int categorId;
    private int page = 1;
    private List<NewsListBean> newsListBeans;
    String title = "";

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new NewsListAdapter(this,baseUrl);
    }

    @Override
    protected String initTitleName() {
        return "单位概况";
    }

    @Override
    public void initView() {
        super.initView();
        setTitle();
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        requestData(true, "");
    }

    @Override
    public void setListener() {
        super.setListener();
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                newsListBeans.get(position).getId();
                HtmlWebViewActivity.show(NewsListActivity.this, title, newsListBeans.get(position).getId());
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                requestData(true, query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void requestData(final boolean isRefresh, String title) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.NEWS_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("categorId", categorId + "");
        map.put("page", page + "");
        map.put("rows", ROWS + "");
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                newsListBeans = GsonUtil.jsonToList(listArray.toString(), NewsListBean.class);
                setData(mAdapter, page, isRefresh, newsListBeans, rows);
            }
        });
    }

    private void setTitle() {
        int titlePosition = getIntent().getIntExtra(TITLE, -1);
        String titleString=getIntent().getStringExtra(TITLE_STRING);
        titleString=titleString!=null?titleString:"";
        if (titlePosition == 0||titleString.equals("单位概况")) {
            title = "单位概况";
            categorId = 7;
        } else if (titlePosition == 1||titleString.equals("党建信息")) {
            title = "党建信息";
            categorId = 3;
        } else if (titlePosition == 2||titleString.equals("公路养护")) {
            title = "公路养护";
            categorId = 6;
        } else if (titlePosition == 3||titleString.equals("收费服务")) {
            title = "收费服务";
            categorId = 4;
        } else if (titlePosition == 4||titleString.equals("安全生产")) {
            title = "安全生产";
            categorId = 8;
        } else {
            title = "访汇聚";
            categorId = 5;
        }
        if(titleString.equals("访汇聚")){
            title = "访汇聚";
            categorId = 5;
        }
        tv_center_title.setText(title);
    }
}
